<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }
        .login-container {
            width: 500px;
            height: 400px;
            padding: 50px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
        }
        .form-group {
            margin-bottom: 15px;
            width: 100%;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .error-message {
            color: red;
            margin-top: 10px;
        }
        .login-button {
            width: 200px;
            height: 50px;
            font-size: 16px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-top: auto;
            transform: scale(1);
            transform-origin: center;
        }
    </style>
</head>
<body>

<div class="login-container">
    <h2>登录</h2>
    <form id="login-form">
        <div class="form-group">
            <label for="id">ID：</label>
            <input type="text" id="id" name="id" required>
        </div>
        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <label for="role">角色：</label>
            <select id="role" name="role">
                <option value="Student">学生</option>
                <option value="User">管理员</option>
                <option value="Instructor">辅导员</option>
            </select>
        </div>
        <div style="flex-grow: 1;"></div>
        <button type="submit" class="login-button">登录</button>
    </form>
    <div id="error-message" class="error-message"></div>
</div>

<script>
    document.getElementById("login-form").addEventListener("submit", function(event) {
        event.preventDefault(); // 防止表单默认提交

        const id = document.getElementById("id").value;
        const password = document.getElementById("password").value;
        const role = document.getElementById("role").value;

        // 构建UserVo对象,不需要手动写id和password，直接用表单值
        const userVo = {
            id: id,
            password: password
        };

        let apiUrl = "";

        // 根据角色选择不同的登录接口
        if (role === "Student") {
            apiUrl = "/api/Student_login";
        } else if (role === "User") {
            apiUrl = "/api/User_login";
        } else if (role === "Instructor") {
            apiUrl = "/api/Instructor_login";
        }

        // 使用Axios发送POST请求到后端
        axios.post(apiUrl, userVo)
            .then(response => {
                if (response.data.code === 1) {
                    // 登录成功
                    alert("登录成功！");
                    // 根据角色进行页面跳转
                    if (role === "Student") {
                        window.location.href = "CM08_student.jsp";
                    } else if (role === "User") {
                        window.location.href = "User.jsp?userid="+ userVo.id;
                    } else if (role === "Instructor") {
                        window.location.href = "instructor.jsp";
                    }
                } else {
                    // 登录失败
                    document.getElementById("error-message").textContent = response.data.message + "失败";
                }
            })
            .catch(error => {
                console.error("登录请求出错：", error);
                document.getElementById("error-message").textContent = "发生错误，请稍后再试。";
            });
    });
</script>
</body>
</html>